package code.ui
{
	import code.utils.TimeUtil;
	
	import com.darwin.darwinDisplay.DarBitmapMovieClip;
	import com.darwin.darwinDll.Dll;
	import com.darwin.darwinDll.core.DllItem;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.manager.base.IToolTip;
	
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import main.App;
	
	import module.building.model.dataObj.BuildingObj;
	import module.main.view.ResourceConst;
	
	/**
	 * 
	 * @author sam
	 *  环形UI基类
	 */	
	public class BaseRingCircle extends UIComponent implements IToolTip
	{
		
		protected var radius:int;//半径
		protected var circleBg:DarBitmapMovieClip;
		protected var startAngle:Number = 270;//起始角度
		protected var itemNum:int = 3;   //组件个数
		protected var singleAngle:Number;  //平均角度
		protected var center:Point = new Point(0,0);//中心坐标
		protected var ringObj:Object;
		private var bgSource:DisplayObject;
		private var itemBgSource:DisplayObject;
		
		public function BaseRingCircle(ringBg:DisplayObject=null,itemSource:DisplayObject=null)
		{
			super();
			bgSource = ringBg;
			itemBgSource = itemSource;
			timeLabelArr = new Array;
			timeArr = new Array;
			timerArr= new Array;
		}
		
		private function init():void{
			this.removeAllChild();
			initBg();
			initNum();
			sortPosition();
		}
		/**
		 * 初始化各类数值 
		 * 
		 */		
		protected function initNum():void{
			singleAngle = 360/itemNum;
			center.x = circleBg.width/2-10;
			center.y = circleBg.height / 2-10;
			radius = circleBg.width/2;
		}
		
		private function initBg():void{
			if(bgSource==null){
				var c:Class = Dll.getInstance().getSynPreloadRes("",ResourceConst.BUILDING_RING,DllItem.TYPE_SWF);
				circleBg = new DarBitmapMovieClip(c);
			}else{
				circleBg = new DarBitmapMovieClip(bgSource);
			}
			addChild(circleBg); 
		}
		
		
		protected function sortPosition():void{
				for(var i:uint=0; i<functionArr.length; i++){
					var sp:BaseRingItem = new BaseRingItem(functionArr[i],itemBgSource); //新建组件
					var angle:uint = i * singleAngle + startAngle; //角度
					var p:Point = getPoint(angle);
					sp.x = p.x; 
					sp.y = p.y; //移动组件
					addChild(sp); //加到舞台

					if(itHasTime(functionArr[i])&&timerArr.length<pushArr.length)
					{
						//var time:int = (ringObj as BuildingObj).currentOrderArr[0]["produce_complete_time"];
						//var serverId:int = (ringObj as BuildingObj).currentOrderArr[0]["serverId"];
						var timeLabel:Label = new Label;
						var time:uint = 12314;
						var timer:Timer = new Timer(1000);
						var obj:Object = new Object();
						obj.timeLabel = timeLabel;
						obj.time = time;
						obj.timer = timer;
						timeLabel.text = TimeUtil.restTimeString(time);
						timeLabel.x = sp.x + sp.width / 2 - timeLabel.width / 2;
						timeLabel.y = sp.y + sp.height;
						addChild(timeLabel);
						timerArr.push(obj);
						timer.addEventListener(TimerEvent.TIMER,timerHandler);
						timer.start();
					}
					else if(itHasTime(functionArr[i]))
					{
						addChild((timerArr[i] as Object).timeLabel);
					}
				}
		}
		private var timeLabelArr:Array;
		private var timeArr:Array;
		private var timerArr:Array;
		private function timerHandler(e:TimerEvent):void
		{
			for(var i:int = 0;i<timerArr.length;i++)
			{
				if(e.currentTarget == timerArr[i].timer)
				{
					(timerArr[i] as Object).time--;
					(timerArr[i] as Object).timeLabel.text = TimeUtil.restTimeString((timerArr[i] as Object).time);
					if((timerArr[i] as Object).time == 0)
					{
						(timerArr[i] as Object).timer.stop();
					}
				}
			}
		}
		private function judgeIsProduce(obj:Object):Boolean
		{
			var $buildingObj:BuildingObj = obj as BuildingObj;
			if($buildingObj.status == 3&&$buildingObj.subType =="0_1")
			{
				return true;
			}
			else
			{
				return false
			}
		}
		/**
		 * 通过度数得到X,Y坐标 
		 * @param angle
		 * 
		 */		
		protected function getPoint(angle:Number):Point{
			var angleRadians:Number = angle * Math.PI / 180; //换算成弧度
			
			var p:Point = new Point(Math.cos(angleRadians) * radius, Math.sin(angleRadians) * radius); //计算偏移量
			
			p.x += center.x; //跟据圆心对偏移量进行修正
			
			p.y += center.y; //跟据圆心对偏移量进行修正
			
			return p; 
		}
		
		public function setData(value:Object,item:*):void{
			if(value is BuildingObj){
				functionArr = getFunctionArr(value);
				ringObj = value;
				itemNum = functionArr.length;
				init();
			}
		}
		private var functionArr:Array;
		private function getFunctionArr(obj:Object):Array
		{
			var $buildObj:BuildingObj = obj as BuildingObj;
			var arr:Array = $buildObj.effectAry;
			var arr1:Array = [];
			//$buildObj.status = 3
			if($buildObj.subType == "0_0"&&$buildObj.status == 3)
			{
				for(var $i:int = 0;$i<arr.length;$i++)
				{
					if(arr[$i] == 12||arr[$i] == 13||arr[$i] == 14)
					{
						arr1.push(arr[$i])
					}
				}
			}
			else if($buildObj.subType == "0_0"&&$buildObj.status == 2)
			{
				for(var $j:int = 0;$j<arr.length;$j++)
				{
					if(arr[$j] == 0||arr[$j] == 1||arr[$j] == 4||arr[$j] == 5||arr[$j] == 6)
					{
						arr1.push(arr[$j])
					}
				}
			}
			else if($buildObj.subType == "0_1"&&$buildObj.status == 3)
			{
				for(var $n:int = 0;$n<arr.length;$n++)
				{
					if(arr[$n] == 11||arr[$n] == 3)
					{
						arr1.push(arr[$n])
					}
				}
			}
			else if($buildObj.subType == "0_1"&&$buildObj.status == 2)
			{
				for(var $s:int = 0;$s<arr.length;$s++)
				{
					if(arr[$s] == 0||arr[$s] == 1 ||arr[$s] == 2||arr[$s] == 3)
					{
						arr1.push(arr[$s])
					}
				}
			}
			else
			{
				return arr;
			}
			return arr1;
		}
		private var pushArr:Array;
		private function itHasTime(i:int):Boolean
		{
			var bo:Boolean = true;
			if(i == 11||i == 12||i == 13||i == 14)
			{
				if(pushArr == null)
				{
					pushArr = [];
					pushArr.push(i);
				}
				else
				{
					for(var j:uint = 0;j<pushArr.length;j++)
					{
						if(pushArr[j] == i)
						{
							bo = false
							break;
						}
					}
					if(bo)
					{
						pushArr.push(i);
					}
				}
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}